package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.basic.wcs.plc.enums.Location;
import com.suray.basic.wcs.plc.enums.PlcDeviceType;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.enums.PlcType;
import com.suray.wcs.service.system.entity.PlcDB;
import com.suray.basic.wcs.plc.entity.PlcDeviceDB;
import com.suray.wcs.service.system.mapper.PlcDBMapper;
import com.suray.basic.wcs.plc.mapper.PlcDeviceDBMapper;
import com.suray.wcs.service.system.service.IPlcDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * plc管理
 *
 * @author zhanghr
 */
@Service
public class PlcDBServiceImpl extends ServiceImpl<PlcDBMapper, PlcDB> implements IPlcDBService {

    @Resource
    private PlcDBMapper plcdbMapper;

    @Autowired
    private PlcDeviceDBMapper plcdevicedbMapper;

    @Override
    public List<PlcDB> selectPlcList(PlcDB plcDB) {
        LambdaQueryWrapper<PlcDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotEmpty(plcDB.getStatus())) {
            lambdaQueryWrapper.eq(PlcDB::getStatus, plcDB.getStatus());
        }
        if (StringUtils.isNotEmpty(plcDB.getPlcName())) {
            lambdaQueryWrapper.like(PlcDB::getPlcName, plcDB.getPlcName());
        }
        lambdaQueryWrapper.orderByAsc(PlcDB::getPlcId);
        return plcdbMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public boolean updatePlc(PlcDB plcdb) {
        return plcdbMapper.updateById(plcdb) == 1;
    }

    @Override
    public boolean addPlc(PlcDB plcdb) {
        plcdb.setCreateBy(SecurityUtils.getUsername());
        plcdb.setCreateTime(new Date());
        boolean result = false;
        int insert = plcdbMapper.insert(plcdb);
        if (insert > 0) {
            result = true;
        }
        if (plcdb.getPlcType().equals(PlcType.PANASONIC.getValue()) || plcdb.getPlcType().equals(PlcType.SIEMENS.getValue()) || plcdb.getPlcType().equals(PlcType.TAMAGAWA.getValue()) ||plcdb.getPlcType().equals(PlcType.FORK_LIFTER.getValue())) {
            PlcDeviceDB plcDeviceDB = new PlcDeviceDB();
            plcDeviceDB.setDeviceName(plcdb.getPlcName());
            plcDeviceDB.setLocation(Location.INSIDE.getCode());
            if (plcdb.getPlcType().equals(PlcType.PANASONIC.getValue())) {
                plcDeviceDB.setDeviceType(PlcDeviceType.PANASONIC_LIFT.getTypeNum());
            }
            if (plcdb.getPlcType().equals(PlcType.SIEMENS.getValue())) {
                plcDeviceDB.setDeviceType(PlcDeviceType.SIEMENS_HOIST.getTypeNum());
            }
            if (plcdb.getPlcType().equals(PlcType.TAMAGAWA.getValue())) {
                plcDeviceDB.setDeviceType(PlcDeviceType.TAMAGAWA_LIFTER.getTypeNum());
            }
            if (plcdb.getPlcType().equals(PlcType.FORK_LIFTER.getValue())) {
                plcDeviceDB.setDeviceType(PlcDeviceType.FORK_LIFTER.getTypeNum());
            }
            plcDeviceDB.setStartAddress(plcdb.getPlcStartNum());
            plcDeviceDB.setCreateTime(new Date());
            plcDeviceDB.setPlcId(plcdb.getPlcId());
            plcDeviceDB.setStatus(plcdb.getStatus());
            plcdevicedbMapper.insert(plcDeviceDB);
        }
        return result;
    }

    /**
     * 查询PLC
     *
     * @param plcName
     * @return
     */
    @Override
    public PlcDB selectPlcByName(String plcName) {
        QueryWrapper<PlcDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PlcDB::getPlcName, plcName);
        return plcdbMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateDisableById(PlcDB plcDB) {
        return plcdbMapper.updateDisableById(plcDB);
    }

    @Override
    public PlcDB selectPlcById(Long plcId) {
        return plcdbMapper.selectById(plcId);
    }

    @Override
    public boolean hasChildByPlcId(Long plcId) {
        LambdaQueryWrapper<PlcDeviceDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PlcDeviceDB::getPlcId, plcId);
        return plcdevicedbMapper.selectList(lambdaQueryWrapper).size() > 0 ? true : false;
    }

    @Override
    public boolean checkPlcNameUnique(PlcDB plcDB) {
        Long plcId = StringUtils.isNull(plcDB.getPlcId()) ? -1L : plcDB.getPlcId();
        PlcDB info = this.selectPlcByName(plcDB.getPlcName());
        if (StringUtils.isNotNull(info) && info.getPlcId().longValue() != plcId.longValue()) {
            return true;
        }
        return false;
    }

    public PlcDB findPlcDBByNetAddress(String ip, int port) {
        LambdaQueryWrapper<PlcDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PlcDB::getPlcIp, ip).eq(PlcDB::getPlcPort, port);
        List<PlcDB> plcDBS = this.list(lambdaQueryWrapper);

        if (plcDBS.size() == 0) {
            return null;
        }

        return plcDBS.get(0);
    }

    public PlcDB getLifterDeviceDB(int lifterType) {
        QueryWrapper<PlcDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PlcDB::getPlcType, lifterType);
        List<PlcDB> plcDBS = plcdbMapper.selectList(queryWrapper);
        if (plcDBS.size() != 0 && plcDBS != null) {
            return plcDBS.get(0);
        }
        throw new RuntimeException("请配置自卸式提升机信息");
    }

    @Override
    public List<PlcDB> selectLifterByStatus(String status) {
        LambdaQueryWrapper<PlcDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(PlcDB::getPlcType, PlcType.PANASONIC, PlcType.SIEMENS, PlcType.TAMAGAWA, PlcType.AUTOMATIC_LIFTER)
                .eq(PlcDB::getStatus, status);
        return list(lambdaQueryWrapper);
    }

    @Override
    public List<PlcDB> getLifterInfosByType(){
        LambdaQueryWrapper<PlcDB> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PlcDB::getPlcType, PlcType.TAMAGAWA);

        return list(lambdaQueryWrapper);
    }
}
